//
//  VKMeMessageController.m
//  V-Learn
//
//  Created by liubo on 2019/1/11.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKMeMessageController.h"
#import "VKMeMessageModel.h"
#import "VKMeMessageCell.h"
#import "VKSystemMessController.h"
#import "VKPlatMessController.h"

@interface VKMeMessageController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isRead;

@end

@implementation VKMeMessageController
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 0.01)];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 0.01)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70;
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"消息";
//    [self tableView];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageIsRead" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isHidenMessage) name:@"MessageIsRead" object:nil];
}
- (void)isHidenMessage{
//    NSLog(@"走通知了");
//    self.isRead = YES;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupLoadFirstData];
}
- (void)setupLoadFirstData{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";
    [hud show:YES];
    NSString *token = [UserModel getUserModel].token;
    VKWeakSelf(self)
    NSMutableDictionary *pagramsD = @{@"token":token == nil ?@"":token}.mutableCopy;
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKMeMessageCount withPostParams:pagramsD withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        [self.dataArray removeAllObjects];
        if ([result[@"code"]integerValue] == 200) {

            for (int i = 0; i < 2; i++) {
                VKMeMessageModel *model = [VKMeMessageModel mj_objectWithKeyValues:result[@"data"]];
                if (i == 0) {
                    model.isSelect = NO;
                }else if (i == 1){
                    model.isSelect = YES;
                }
                [self.dataArray addObject:model];
            }
        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
        [weakself.tableView reloadData];
    }];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count == 0 ? 0 : 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 12)];
        return view;
    }else if (section == 1){
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 6)];
        return view;
    }
    
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 12;
    }else if(section == 1){
        return 6;
    }
    return 0.1;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 0.01)];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKMeMessageCell *cell = [VKMeMessageCell cellWithTableView:tableView];
    VKMeMessageModel *model = self.dataArray[indexPath.section];
    if (indexPath.section == 0) {
        model.content = @"系统消息";
        model.imageString = @"xtxx";
//        cell.isRead = self.isRead;
        cell.model = model;
        
    }else if (indexPath.section == 1){
        model.content = @"平台消息";
        model.imageString = @"ptxx";
        cell.model = model;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        self.isRead = YES;
        [self.navigationController pushViewController:[[VKSystemMessController alloc]init] animated:YES];
    }else if (indexPath.section == 1){
        self.isRead = YES;
        [self.navigationController pushViewController:[[VKPlatMessController alloc]init] animated:YES];
    }
}
@end
